package com.tarena.micro.service.cart.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.tarena.micro.service.cart.service.CartService;
import com.tarena.micro.service.common.result.JsonResult;
import com.tarena.micro.service.pojo.param.CartAddParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * 处理所有和购物车相关的业务接口
 */
//组合注解 = @Controller @ResponseBody
@RestController
public class CartController {
    @Autowired
    private CartService cartService;
    //新增购物车
    @PostMapping("/cart/add")
    public JsonResult addCart(CartAddParam param) {
        //调用业务层
        System.out.println("接到参数:"+param);
        cartService.addCart(param);
        //使用JsonResult 不需要携带查询数据返回 直接返回
        return JsonResult.ok();//和前端约定好的结果 {"state":200,"message":"ok","data":null}
    }
    //删除购物车
    @PostMapping("/cart/delete")
    public JsonResult deleteCart(@RequestParam("productCode")String productCode,
                                 @RequestParam("userId")String userId){
        cartService.deleteCart(productCode,userId);
        return JsonResult.ok();
    }
    @Value("${server.port}")
    private String port;
    @GetMapping("/cart/hello")
    public String sayHello(@RequestHeader("X-Request-Color") String value){
        if (StringUtils.isNotEmpty(value)){
            //说明头传值了
            return "hello gateway,header is "+value+", and i am from "+port;
        }
        return "hello gateway i am from "+port;
    }
}
